import requests 
import json
import pandas as pd
class investment_portfolio_api:
    '''
    小果投资组合api
    '''
    def __init__(self,stock_list=['600031','600111'],start_date='2021-01-01',end_date='2050-01-01',
                 url='http://120.78.132.143',port=8023,password='123456',):
        '''
        小果投资组合api
        '''
        self.url=url
        self.port=port
        self.password=password
        self.start_date=start_date
        self.end_date=end_date
        self.stock_list=stock_list
    def get_add_user_def_stock(self,stock='600031'):
        '''
        添加自定义标的
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"show_table.data","outputs":{"id":"show_table","property":"data"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"自定义"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"添加"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"user_def_stock","property":"value","value":"{}".format(stock)}],
            "changedPropIds":["portfolio_analysis_add_data.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame(text['response']['show_table']['data'])
        return df
    def get_del_user_def_stock(self):
        '''
        清空自定义标的
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"show_table.data","outputs":{"id":"show_table","property":"data"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"自定义"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"清空"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"user_def_stock","property":"value","value":"600031"}],
            "changedPropIds":["portfolio_analysis_add_data.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame(text['response']['show_table']['data'])
        return df
    def get_add_all_user_def_stock(self):
        '''
        批量添加分析标的
        '''
        try:
            self.get_del_user_def_stock()
        except:
            pass
        for stock in self.stock_list:
            print(self.get_add_user_def_stock(stock=stock))
    def get_loads_all_data(self):
        '''
        加载全部数据
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"show_table_1.data","outputs":{"id":"show_table_1","property":"data"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"不分析"}],
            "changedPropIds":["portfolio_analysis_update.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame(text['response']['show_table_1']['data'])
        return df
    def get_analysis_table_data(self):
        '''
        获取分析指标表
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"show_table_1.data","outputs":{"id":"show_table_1","property":"data"},
        "inputs":[{"id":"password","property":"value","value":"123456"},
        {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
        {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
        {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
        {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
        {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
        {"id":"portfolio_analysis_run","property":"value","value":"运行"},
        {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
        {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
        {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
        {"id":"trader_analysis","property":"value","value":"分析"}],
        "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame(text['response']['show_table_1']['data'])
        return df
    def get_porffolio_analysis_data(self):
        '''
        获取组合收益分析
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"cum_return.figure","outputs":{"id":"cum_return","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['cum_return']['figure']['data'][0]['x']
        df['return']=text['response']['cum_return']['figure']['data'][0]['y']
        df['index_return']=text['response']['cum_return']['figure']['data'][1]['y']
        return df
    def get_annual_income_distribution(self):
        '''
        获取组合年度分布
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"annual_income_distribution.figure",
            "outputs":{"id":"annual_income_distribution","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['annual_income_distribution']['figure']['data'][0]['x']
        df['return']=text['response']['annual_income_distribution']['figure']['data'][0]['y']
        df['index_return']=text['response']['annual_income_distribution']['figure']['data'][1]['y']
        return df
    def get_moth_income_distribution(self):
        '''
        获取月度收益分布
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"moth_income_distribution.figure",
            "outputs":{"id":"moth_income_distribution","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['moth_income_distribution']['figure']['data'][0]['x']
        df['return']=text['response']['moth_income_distribution']['figure']['data'][0]['y']
        df['index_return']=text['response']['moth_income_distribution']['figure']['data'][1]['y']
        return df
    def get_daily_income_distribution(self):
        '''
        日度收益
        ''' 
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"daily_income_distribution.figure",
            "outputs":{"id":"daily_income_distribution","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['daily_income_distribution']['figure']['data'][0]['x']
        df['return']=text['response']['daily_income_distribution']['figure']['data'][0]['y']
        df['index_return']=text['response']['daily_income_distribution']['figure']['data'][1]['y']
        return df
    def get_rolling_volatility(self):
        '''
        滚动波动率
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"rolling_volatility.figure",
            "outputs":{"id":"rolling_volatility","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['rolling_volatility']['figure']['data'][0]['x']
        df['portfolio']=text['response']['rolling_volatility']['figure']['data'][0]['y']
        df['index']=text['response']['rolling_volatility']['figure']['data'][1]['y']
        return df
    def get_rolling_shape(self):
        '''
        滚动是夏普
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"rolling_shape.figure","outputs":{"id":"rolling_shape","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['rolling_shape']['figure']['data'][0]['x']
        df['portfolio']=text['response']['rolling_shape']['figure']['data'][0]['y']
        df['index']=text['response']['rolling_shape']['figure']['data'][1]['y']
        return df
    def get_rolling_nax_down(self):
        '''
        滚动是最大回撤
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"max_down.figure","outputs":{"id":"max_down","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['max_down']['figure']['data'][0]['x']
        df['portfolio']=text['response']['max_down']['figure']['data'][0]['y']
        df['index']=text['response']['max_down']['figure']['data'][1]['y']
        return df
    def get_rolling_sortino_ratio(self):
        '''
        滚动索提诺比率
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"rolling_sortino_ratio.figure",
            "outputs":{"id":"rolling_sortino_ratio","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['rolling_sortino_ratio']['figure']['data'][0]['x']
        df['portfolio']=text['response']['rolling_sortino_ratio']['figure']['data'][0]['y']
        df['index']=text['response']['rolling_sortino_ratio']['figure']['data'][1]['y']
        return df
    def get_roll_up_down_capture(self):
        '''
        捕获比例
        '''
        url='{}:{}/_dash-update-component'.format(self.url,self.port)
        headers={'Content-Type':'application/json'}
        data={"output":"roll_up_down_capture.figure",
            "outputs":{"id":"roll_up_down_capture","property":"figure"},
            "inputs":[{"id":"password","property":"value","value":"123456"},
            {"id":"portfolio_analysis_run_type","property":"value","value":"选择"},
            {"id":"portfolio_analysis_data_type","property":"value","value":"外盘ETF"},
            {"id":"portfolio_analysis_add_data","property":"value","value":"等待"},
            {"id":"portfolio_analysis_start_date","property":"date","value":"{}".format(self.start_date)},
            {"id":"portfolio_analysis_end_date","property":"date","value":"{}".format(self.end_date)},
            {"id":"portfolio_analysis_run","property":"value","value":"运行"},
            {"id":"portfolio_analysis_down_data","property":"value","value":"不下载"},
            {"id":"portfolio_analysis_update","property":"value","value":"不加载"},
            {"id":"portfolio_analysis_name_list","property":"value","value":"日经ETF"},
            {"id":"trader_analysis","property":"value","value":"分析"}],
            "changedPropIds":["trader_analysis.value"]}
        res=requests.post(url=url,data=json.dumps(data),headers=headers)
        text=res.json()
        df=pd.DataFrame()
        df['date']=text['response']['roll_up_down_capture']['figure']['data'][0]['x']
        df['portfolio']=text['response']['roll_up_down_capture']['figure']['data'][0]['y']
        df['index']=text['response']['roll_up_down_capture']['figure']['data'][1]['y']
        return df
    def get_all_analysis_result(self):
        '''
        获取全部的分析结果
        '''
        #添加全部股票
        self.get_add_all_user_def_stock()
        #加载全部数据
        df=self.get_loads_all_data()
        print(df)
        #分析表
        tabel=self.get_analysis_table_data()
        print(tabel)
        #后面看函数get_porffolio_analysis_data
if __name__=='__main__':
    '''
    投资组合分析api
    '''
    models=investment_portfolio_api(stock_list=['510880','511010','511220','518880',
                    '164824','513520','513030','513080','513100'
                        ],
                start_date='2010-01-01',end_date='2050-01-01',
                 url='http://120.78.132.143',port=8023,password='123456')
    print(models.get_all_analysis_result())